package com.j48.entrancegurad.controller;

import com.j48.entrancegurad.model.Picture;
import com.j48.entrancegurad.service.IPictureService;
import net.minidev.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.util.*;

@Controller
public class PictureController {
    @Autowired
    private IPictureService pictureService;

    /**
     * 修改图片
     * @param file
     * @param request
     * @return
     */
    @RequestMapping("/uploadPicture")
    @ResponseBody
    public String uploadPicture(MultipartFile file,HttpServletRequest request){
        String picture_id1=request.getParameter("picture_id");
        int picture_id=Integer.parseInt(picture_id1);
        String s=pictureService.uploadImg(request,picture_id,file);
        if(s==null||s.equals("")){
            return "false";
        }
        flush(request);
        return "true";
    }


    /**
     * 更新session中的图片列表
     * @param request
     */
    public void flush(HttpServletRequest request){
        request.getSession().removeAttribute("pictureList");
        List<Picture> pictureList=new ArrayList<Picture>();
        pictureList=pictureService.selectAllAble();
        request.getSession().setAttribute("pictureList",pictureList);
    }

    /**
     * 删除图片
     * @param req
     * @return
     */
    @RequestMapping("/deletePicture")
    @ResponseBody
    public String deletePicture(HttpServletRequest req){
        int picture_id=Integer.parseInt(req.getParameter("picture_id"));
        Picture picture=new Picture();
        picture.setPicture_id(picture_id);
        if(pictureService.delete(picture)){
            flush(req);
            return "true";
        }
        return "false";
    }

    /**
     * 更新session中的图片列表
     * @param request
     * @return
     */
    @RequestMapping("/flushPicture")
    @ResponseBody
    public ModelAndView flushPicture(HttpServletRequest request){
        ModelAndView model=null;
        flush(request);
        model= new ModelAndView("jsp/pictureList");
        return model;
    }

    /**
     * 添加图片
     * @param file
     * @param request
     * @return
     */
    @RequestMapping("/addPicture")
    @ResponseBody
    public String addPicture(MultipartFile file,HttpServletRequest request){
        String pat=new java.io.File(request.getSession().getServletContext().getRealPath(request.getRequestURI())).getParent();
        String s=pictureService.addPicture(file,pat);
        if(s==null||s.equals("")){
            return "false";
        }
        flush(request);
        return "true";
    }
}



